<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    class="site-dialog"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px" @keyup.enter.native="dataFormSubmit()">
      <el-row>
        <el-col>
          <el-form-item label="商品" prop="goodsId">
            <el-input v-model="dataForm.goodsId" placeholder="商品ID" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="闪购价格" prop="price">
            <el-input v-model="dataForm.price" placeholder="闪购价格" @blur="inputMoney($event,'price')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="闪购状态" prop="status">
            <el-input v-model="dataForm.status" placeholder="闪购状态" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="开始时间" prop="startdate">
            <el-input v-model="dataForm.startdate" placeholder="开始时间" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="结束时间" prop="enddata">
            <el-input v-model="dataForm.enddata" placeholder="结束时间" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataForm.remark" placeholder="备注" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col style="text-align: right;">
          <el-button size="mini" icon="el-icon-close" @click="visible = false">取消</el-button>
          <el-button size="mini" icon="el-icon-check" type="primary" @click="dataFormSubmit()">确定</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { getInputValue } from '@/utils/money'
import { isDecimal } from '@/utils/validate'
export default {
  data() {
    var isvalidDecimal = (rule, value, callback) => {
      if (!isDecimal(value)) {
        callback(new Error('请输入数字值'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      dataForm: {
        flashPurchaseId: 0,
        goodsId: '',
        shopId: '',
        price: '',
        status: '',
        startdate: '',
        enddata: '',
        remark: '',
        createtime: '',
        creator: '',
        updatetime: '',
        updater: '',
        flgdata: ''
      },
      dataRule: {
        goodsId: [
          { required: true, message: '商品ID不能为空', trigger: 'blur' }
        ],
        shopId: [
          { required: true, message: '店铺ID不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '闪购价格不能为空', trigger: 'blur' },
          { validator: isvalidDecimal, trigger: 'blur' }
        ],
        status: [
          { required: true, message: '闪购状态不能为空', trigger: 'blur' }
        ],
        startdate: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        enddata: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: '备注不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    inputMoney(el, name) {
      this.dataForm[name] = getInputValue(el)
    },
    init(id) {
      this.dataForm.flashPurchaseId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.flashPurchaseId) {
          this.$http({
            url: this.$http.adornUrl(`/shopservice/yfcloudgoodsflashpurchase/info/${this.dataForm.flashPurchaseId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.goodsId = data.yfcloudGoodsFlashPurchase.goodsId
              this.dataForm.shopId = data.yfcloudGoodsFlashPurchase.shopId
              this.dataForm.price = data.yfcloudGoodsFlashPurchase.price
              this.dataForm.status = data.yfcloudGoodsFlashPurchase.status
              this.dataForm.startdate = data.yfcloudGoodsFlashPurchase.startdate
              this.dataForm.enddata = data.yfcloudGoodsFlashPurchase.enddata
              this.dataForm.remark = data.yfcloudGoodsFlashPurchase.remark
              this.dataForm.createtime = data.yfcloudGoodsFlashPurchase.createtime
              this.dataForm.creator = data.yfcloudGoodsFlashPurchase.creator
              this.dataForm.updatetime = data.yfcloudGoodsFlashPurchase.updatetime
              this.dataForm.updater = data.yfcloudGoodsFlashPurchase.updater
              this.dataForm.flgdata = data.yfcloudGoodsFlashPurchase.flgdata
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/shopservice/yfcloudgoodsflashpurchase/${!this.dataForm.flashPurchaseId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'flashPurchaseId': this.dataForm.flashPurchaseId || undefined,
              'goodsId': this.dataForm.goodsId,
              'shopId': this.dataForm.shopId,
              'price': this.dataForm.price,
              'status': this.dataForm.status,
              'startdate': this.dataForm.startdate,
              'enddata': this.dataForm.enddata,
              'remark': this.dataForm.remark,
              'createtime': this.dataForm.createtime,
              'creator': this.dataForm.creator,
              'updatetime': this.dataForm.updatetime,
              'updater': this.dataForm.updater,
              'flgdata': this.dataForm.flgdata
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
